from django.db import models

# Create your models here.

class Publisher(models.Model):
    #出版社 [一]
    name = models.CharField('出版社名称', max_length=50)

class Book(models.Model):
    #书名 [多]
    title = models.CharField('书名', max_length=11)
    publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE)

# >>> from otm.models import *
# >>> p1 = Publisher.objects.create(name='中信出版社')
# >>> p1
# <Publisher: Publisher object (2)>
# >>> b1 = Book.objects.create(title='Python1',publisher=p1)
# >>> b2 = Book.objects.create(title='Python2',publisher_id=1)
#正向查询：
# >>> b1.publisher
# <Publisher: Publisher object (2)>
# >>> b1.publisher.name
# '中信出版社'
#反向查询
# >>> p1 = Publisher.objects.get(name='中信出版社')
# >>> p1
# <Publisher: Publisher object (1)>
# >>> b1 = p1.book_set
# >>> b1
# <django.db.models.fields.related_descriptors.create_reverse_many_to_one_manager.<locals>.RelatedManage
# >>> b1.all()
# <QuerySet [<Book: Book object (1)>, <Book: Book object (3)>]>



